package com.yyq.adms.yyb.patent.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.util.JxlsUtil;
import com.yyq.adms.util.JxlsVo;
import com.yyq.adms.util.ValidateUtil;
import com.yyq.adms.util.ValidateVo;
import com.yyq.adms.yyb.patent.bpo.OwnPatentBpo;
import com.yyq.adms.yyb.patent.pojo.OwnPatent;
import com.yyq.adms.yyb.utils.YYBUtils;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.PermissionsUtil;
import com.yyq.sos.util.page.PageFormData;


/**
 * 获批专利Action 层
 * @author 杨应滨
 * @2012-12-3
 *
 */
@Controller("ownPatentAction")
@Scope("prototype")
public class OwnPatentAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private OwnPatent po = new OwnPatent();
	
	private OwnPatentBpo bpo;

	public OwnPatent getPo() {
		return po;
	}

	public void setPo(OwnPatent po) {
		this.po = po;
	}

	public OwnPatentBpo getBpo() {
		return bpo;
	}

	@Resource(name="ownPatentBpo")
	public void setBpo(OwnPatentBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	
	public String execute()throws Exception
	{
		super.setBpo(bpo);
		super.setObj(po);
		String s = super.execute();
		this.setPo((OwnPatent)super.getObj());
		return s;
	}
	
	
	@Override
	public String data() {
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			hql = "from OwnPatent order by catchDate desc";
		}else{
			hql = "from "+po.getClass().getName()+" obj where obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+")  order by obj.catchDate desc";
		}
		this.setFormData(bpo.searchPage(hql, getPageForm().getPageNo(), getPageForm().getPageSize()));
		
		processData();
		
		return ActionType.ACTION_DATA_LIST;
	}
	
	/*@Override
	public String batchAdd() {
		if(this.getAccessory() != null){
			String msg = "";
			if("xls".equals(this.getAccessoryFileName().substring(this.getAccessoryFileName().lastIndexOf(".")+1))){
				try {
					List<Map<String, JxlsVo>> data = JxlsUtil.getInstance().transfromData(this.getAccessory());
					Map<String,ValidateVo> vMap = ValidateUtil.getInstance().getValidatesByClassName(po.getClass().getName());
					List list = new ArrayList();
					LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
					List repeatDB = new ArrayList();
					StringBuffer errMsg = JxlsUtil.getInstance().validateXls(data, vMap, po.getClass(), list,user,repeatDB);
					List otherRepertData = bpo.validateUniquene(data, errMsg);
					msg = errMsg.toString();
					if(msg.toString().equals("")){
						if(list.size()>0){
							Serializable[] ids = bpo.save(list.toArray());
							if(ids != null){
								msg = "添加成功！";
							}else{
								msg = "添加失败！";
							}
						}else{
							msg = "批量添加数据为空，请添加!";
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				this.setMsg(msg);
			}else{
				this.setMsg("文件类型不支持!");
			}
		}
		return ActionType.ACTION_BATCH_ADD;
	}*/

	private void processData() {
		PageFormData formData= this.getFormData();
		List _templist = formData.getData();
		List _list = new ArrayList();
		for(int i = 0; i < _templist.size(); i ++) {
			OwnPatent _declare = (OwnPatent)_templist.get(i);
			
			Date _date = _declare.getCatchDate();
			
			String _year = YYBUtils.getFormatDateYear(_date);
			_declare.setBelongYear(_year);
			_list.add(_declare);
		}
		if(_list.size() > 0) 
			formData.setData(_list);
	}
	
	public String search()
	{
		String searchProperty = ((String[])this.getFormValue("searchProperty"))[0];
		String searchKey = ((String[])this.getFormValue("searchKey"))[0];
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			if("sentDep".equals(searchProperty)){
				hql = "select distinct obj from "+po.getClass().getName()+" obj,RecordSentDep rsd,SysDepts d where " +
					  " obj.id=rsd.sourceRecordId and rsd.sentDepId=d.id and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and d.deptCname like '%"+searchKey+"%' order by catchDate desc";
			}else{
				hql = "from "+po.getClass().getName()+" where "+searchProperty+" like '%"+searchKey+"%' order by catchDate desc";
			}
		}else{
			hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+")) and obj."+searchProperty+" like '%"+searchKey+"%' order by obj.catchDate desc";
		}
		this.setFormData(bpo.searchPage(hql, this.getPageForm().getPageNo(), this.getPageForm().getPageSize()));
		processData();
		return ActionType.ACTION_DATA_LIST;
	}
	

}
